﻿using MySoftLab.DapFlashCore.CoreSight;
using MySoftLab.DapFlashCore.Flash;
using MySoftLab.DapFlashCore.Probes.DapAccess;
using MySoftLab.DapFlashCore.Targets;
using MySoftLab.DapFlashCore.Targets.Packs;

namespace MySoftLab.DapFlashCore
{
    static class TestClass
    {
        static void TestProgramL471()
        {
            DebugPort dp;

            var daplinks = DapAccessCmsisDap.GetConnectedDevices();

            var daplink = daplinks.FirstOrDefault();

            if (daplink != null)
            {
                daplink.Open();
                daplink.Close();
                daplink.Open();

                // 设置clock
                daplink.SetClock(1000000);

                dp = new DebugPort(daplink);
                dp.Init();
                dp.PowerUpDebug();

                var discovery = new Discovery(dp);
                discovery.Discover();

                var ap = dp.Aps.Values.First();

                var cpu = new CortexM(daplink, dp, ap);
                cpu.ReadCoreType();

                var flash = new FlashMemory(cpu, STM32L471.Instance);
                flash.Init(FlashOperations.PROGRAM);

                // 加载数据
                var fileBuf = File.ReadAllBytes("app.bin");
                flash.Program(fileBuf, 0x08000000);

                flash.Cleanup();

                Console.WriteLine("program ok");
            }
        }

        static void TestProgramL010()
        {
            DebugPort dp;

            var daplinks = DapAccessCmsisDap.GetConnectedDevices();

            var daplink = daplinks.FirstOrDefault();

            if (daplink != null)
            {
                daplink.Open();
                daplink.Close();
                daplink.Open();

                // 设置clock
                daplink.SetClock(1000000);

                dp = new DebugPort(daplink);
                dp.Init();
                dp.PowerUpDebug();

                var discovery = new Discovery(dp);
                discovery.Discover();

                var ap = dp.Aps.Values.First();

                var cpu = new CortexM(daplink, dp, ap);

                var target = PackExtracter.ExtractTarget(@"E:\迅雷下载\Keil.STM32L0xx_DFP.2.3.0.pack", "STM32L010C6");

                var flash = new FlashMemory(cpu, target);
                flash.Init(FlashOperations.PROGRAM);

                // 加载数据
                var fileBuf = File.ReadAllBytes("app.bin");
                flash.Program(fileBuf, 0x08000000);

                flash.Cleanup();

                Console.WriteLine("program ok");
            }
        }

        static void TestProgramL471Flm()
        {
            DebugPort dp;

            var daplinks = DapAccessCmsisDap.GetConnectedDevices();

            var daplink = daplinks.FirstOrDefault();

            if (daplink != null)
            {
                daplink.Open();
                daplink.Close();
                daplink.Open();

                // 设置clock
                daplink.SetClock(1000000);

                dp = new DebugPort(daplink);
                dp.Init();
                dp.PowerUpDebug();   // create_connect_sequence
                                     // unlock_device sequence

                var discovery = new Discovery(dp);
                discovery.Discover();

                var ap = dp.Aps.Values.First();

                var cpu = new CortexM(daplink, dp, ap);

                var target = PackExtracter.ExtractTarget(@"E:\迅雷下载\Keil.STM32L4xx_DFP.2.7.0.pack", "STM32L471VG");

                var flash = new FlashMemory(cpu, target);
                flash.Init(FlashOperations.PROGRAM);

                // 加载数据
                var fileBuf = File.ReadAllBytes("app.bin");
                flash.Program(fileBuf, 0x08000000);

                flash.Cleanup();

                Console.WriteLine("program ok");
            }
        }

        static void TestProgramL151RET6()
        {
            DebugPort dp;

            var daplinks = DapAccessCmsisDap.GetConnectedDevices();

            var daplink = daplinks.FirstOrDefault();

            if (daplink != null)
            {
                daplink.Open();
                daplink.Close();
                daplink.Open();

                // 设置clock
                daplink.SetClock(1000000);

                dp = new DebugPort(daplink);
                dp.Init();
                dp.PowerUpDebug();   // create_connect_sequence
                                     // unlock_device sequence

                var discovery = new Discovery(dp);
                discovery.Discover();

                var ap = dp.Aps.Values.First();

                var cpu = new CortexM(daplink, dp, ap);

                var target = PackExtracter.ExtractTarget(@"E:\迅雷下载\Keil.STM32L1xx_DFP.1.4.1.pack", "STM32L151RE");

                var flash = new FlashMemory(cpu, target);
                flash.Init(FlashOperations.PROGRAM);

                // 加载数据
                var fileBuf = File.ReadAllBytes("app.bin");
                flash.Program(fileBuf, 0x08000000);

                flash.Cleanup();

                Console.WriteLine("program ok");
            }
        }

        static void TestElf()
        {
            FlashAlgoExtracter extracter = new FlashAlgoExtracter();
            var algo = extracter.Load("STM32L1xx_512.FLM");
            Console.WriteLine(algo);
            extracter.Load(@"E:\迅雷下载\Keil.STM32L0xx_DFP.2.3.0\CMSIS\Flash\STM32L0xx_16.FLM");
            extracter.Load("STM32F4xx_1024.FLM");

        }

        static void TestExtractPack(string path, string targetName)
        {
            var target = PackExtracter.ExtractTarget(path, targetName);
            if (null == target)
            {
                Console.WriteLine("failed " + path);
            }
        }

        static void TestMothod()
        {
            TestExtractPack(@"E:\迅雷下载\Keil.STM32L4xx_DFP.2.7.0.pack", "STM32l471VG");
            TestExtractPack(@"E:\迅雷下载\Keil.STM32F4xx_DFP.3.0.0.pack", "STM32F469AE");
            TestExtractPack(@"E:\迅雷下载\Keil.STM32L1xx_DFP.1.4.1.pack", "STM32L151RE");
            TestExtractPack(@"E:\迅雷下载\Keil.STM32L0xx_DFP.2.3.0.pack", "STM32L010C6");

            // TestElf();
            // TestProgramL010();
            Console.WriteLine("press any key");
            Console.ReadLine();
            // TestProgramL010();
            // TestProgramL471Flm();
            // TestProgramL471();
            // TestProgramL151RET6();
        }
    }
}


